package com.shop.cereshop.admin.service.im.impl;

import cn.hutool.core.collection.CollUtil;
import com.shop.cereshop.admin.dao.buyer.CereBuyerUserDAO;
import com.shop.cereshop.admin.dao.user.CerePlatformUserDAO;
import com.shop.cereshop.admin.page.im.ImSingleMsgVO;
import com.shop.cereshop.admin.service.im.ImService;
import com.shop.cereshop.commons.domain.buyer.CereBuyerUser;
import com.shop.cereshop.commons.domain.im.tx.req.msg.MsgBody;
import com.shop.cereshop.commons.domain.im.tx.req.msg.MsgInfoReq;
import com.shop.cereshop.commons.domain.im.tx.req.msg.UserSendMsgReq;
import com.shop.cereshop.commons.domain.im.tx.req.user.AccountImportReq;
import com.shop.cereshop.commons.domain.im.tx.req.user.AccountListImportReq;
import com.shop.cereshop.commons.domain.im.tx.resp.friend.FriendBaseResp;
import com.shop.cereshop.commons.domain.im.tx.resp.user.ImUserBaseResp;
import com.shop.cereshop.commons.domain.user.CerePlatformUser;
import com.shop.cereshop.commons.service.im.ImUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service("imService")
public class ImServiceImpl implements ImService {
    @Resource
    private CereBuyerUserDAO cereBuyerUserDAO;
    @Resource
    private ImUserService imUserService;
    @Resource
    private CerePlatformUserDAO cerePlatformUserDAO;

//    @Resource
//    private CereImReportDAO cereImReportDAO;

    @Override
    public Integer pushUserInfo() {
        List<CereBuyerUser> allBuyerUser = cereBuyerUserDAO.getAllBuyerUser();
        List<CerePlatformUser> allShopUser = cerePlatformUserDAO.getAllShopUser();
        if (CollUtil.isEmpty(allBuyerUser) && CollUtil.isEmpty(allShopUser)) {
            return 0;
        }
        AccountImportReq accountImportReq;
        List<AccountImportReq> accountList = new ArrayList<>();
        AccountListImportReq accountListImportReqs = new AccountListImportReq();
        for (CereBuyerUser buyerUser : allBuyerUser) {
            accountImportReq = AccountImportReq.builder()
                    .UserID(buyerUser.getBuyerUserId().toString())
                    .Nick(buyerUser.getName())
                    .FaceUrl(buyerUser.getHeadImage())
                    .build();
            accountList.add(accountImportReq);
        }
        for (CerePlatformUser cerePlatformUser : allShopUser) {
            accountImportReq = AccountImportReq.builder()
                    .UserID(cerePlatformUser.getPlatformUserId().toString())
                    .Nick(cerePlatformUser.getUsername())
                    .FaceUrl(cerePlatformUser.getAvatar())
                    .build();
            accountList.add(accountImportReq);
        }
        accountListImportReqs.setAccountList(accountList);
        ImUserBaseResp imUserBaseResp = imUserService.multiAccountImport(accountListImportReqs);
        if ("OK".equals(imUserBaseResp.getActionStatus())) {
            return allBuyerUser.size() - imUserBaseResp.getFailAccounts().size();
        }
        return 0;
    }

    @Override
    public void sendMsg(String fromAccount, List<MsgBody> msgBodyList) {
        MsgInfoReq req = new MsgInfoReq();
        req.setSyncOtherMachine(2);
        req.setTo_Account(fromAccount);
        req.setMsgBody(msgBodyList);
        FriendBaseResp friendItemFriendBaseResp = imUserService.sendMsg(req);
        log.info("发送消息结果：{}", friendItemFriendBaseResp);
    }


    @Override
    public void callbackBeforeSendMsg(UserSendMsgReq req) {

    }

    @Override
    public ImSingleMsgVO getSingleMsg(String fromAccount, String toAccount) {
        return null;
    }

}
